/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.postorder.manger;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.lxk.oms.postorder.domain.req.AuditPostOrderReq;
import com.lxk.oms.postorder.domain.req.CreatePostOrderReq;
import com.lxk.oms.postorder.domain.req.RefundOrderReq;
import com.lxk.oms.postorder.flow.audit.AuditNameConst;
import com.lxk.oms.postorder.flow.audit.PoAuditContext;
import com.lxk.oms.postorder.flow.craete.FlowPostOrderNameConst;
import com.lxk.oms.postorder.flow.craete.PostOrderCreateContext;
import com.lxk.oms.postorder.flow.refund.FlowRefundPostOrderNameConst;
import com.lxk.oms.postorder.flow.refund.PostOrderRefundContext;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.AllArgsConstructor;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.springframework.stereotype.Service;

/**
 * 售后订单
 *
 * @author heyi
 * 2024/9/28
 */
@Service
@AllArgsConstructor
public class PostOrderManger {
    private final FlowExecutor flowExecutor;


    /**
     * 售后订单创建
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public PostOrderCreateContext.CreatePostOrderResult createPostOrder(CreatePostOrderReq req) {
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowPostOrderNameConst.CREATE_POST_ORDER, req, PostOrderCreateContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(PostOrderCreateContext.class).getResult();
    }


    /**
     * 售后订单-仅退款
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public PostOrderRefundContext.RefundResult refundPostOrder(RefundOrderReq req) {
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(FlowRefundPostOrderNameConst.REFUND_POST_ORDER, req, PostOrderRefundContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        return liteflowResponse.getContextBean(PostOrderRefundContext.class).getResult();
    }

    /**
     * 创建售后订单-仅退款-直接退款成功（商家尚未发货，用户直接取消订单）
     *
     * @param req
     */
    @DSTransactional(rollbackFor = Exception.class)
    public PostOrderRefundContext.RefundResult createAndRefundPostOrder(CreatePostOrderReq req) {
        PostOrderManger orderManger = SpringUtils.getAopProxy(this);
        PostOrderCreateContext.CreatePostOrderResult postOrder = orderManger.createPostOrder(req);
        RefundOrderReq req2 = new RefundOrderReq();
        req2.setPostOrderNo(postOrder.getPostOrderBo().getPostOrderNo());
        req2.setUserType(UserType.APP_USER.getUserType());
        req2.setUserId(postOrder.getPostOrderBo().getUserId());
        return orderManger.refundPostOrder(req2);
    }

    /**
     * 审核接口退款订单
     * 用户发起后-商家审核退款订单-且把商家的收货地址填充
     * 如果拒绝，需要把申请可售后数量返回给对应的子订单
     */
    @DSTransactional(rollbackFor = Exception.class)
    public Boolean auditPostOrder(AuditPostOrderReq req) {
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(AuditNameConst.PO_AUDIT_NAME, req, PoAuditContext.class);
        if (!liteflowResponse.isSuccess()) {
            throw new ServiceException(liteflowResponse.getMessage());
        }
        PoAuditContext contextBean = liteflowResponse.getContextBean(PoAuditContext.class);
        return contextBean.getResult();
    }

    /**
     * 审核成功后，用户商品邮寄给商家，并且填写快递单号
     */
    public void sendPostOrder() {

    }

    /**
     * 商家收到货之后，确认收货
     */
    public void confirmPostOrder() {

    }

    /**
     * 跟进售后订单进行退款
     */
    public void refundPostOrder() {

    }
}
